<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">

    <ui:composition template="layout.xhtml">
        <ui:define name="body">

            <h:form id="dataForm" enctype="multipart/form-data">
                <br/>
                <h3 class="title">Cadastrar Memorial Descritivo</h3>
                <p:growl id="messages" showDetail="true" autoUpdate="true"  /> 
                <div class="row">
                    <div class="twelve columns"> 
                        <p:panel id="dataPanel" style="background: none; border: none;" 
                                 styleClass="container_24 clearfix both ">
                            <fieldset>
                                <legend> <h4 style="margin:0 0 10px 0">Dados Gerais</h4></legend>
                                <p:outputLabel value="Nº Processo:" styleClass="grid_3"/>
                                <p:inputText styleClass="grid_8" value="#{managerCreateDataSheet.dataSheet.n_process}" />
                                
                                <p:outputLabel value="Tipo:" styleClass="grid_3"/> 
                                <div class="grid_8">
                                    <p:selectOneMenu panelStyle="width:150px;" value="#{managerCreateDataSheet.dataSheet.type_transference}">
                                        <f:selectItem itemLabel="" itemValue=""/>
                                        <f:selectItems value="#{managerCreateDataSheet.typeTransference}"/>  
                                    </p:selectOneMenu>
                                </div>

                                <p:outputLabel value="Assunto: " styleClass="grid_3"/>
                                <p:inputText styleClass="grid_20 ajustInput" value="#{managerCreateDataSheet.dataSheet.topic}" />
                                
                                <p:outputLabel value="Lote: " styleClass="grid_3"/>
                                <p:inputText styleClass="grid_8" value="#{managerCreateDataSheet.dataSheet.lote}" />
                                
                                <p:outputLabel value="Rip: " styleClass="grid_3"/>
                                <p:inputText styleClass="grid_8" value="#{managerCreateDataSheet.dataSheet.rip}" />
                                
                                <p:outputLabel value="Obs: " styleClass="grid_3"/>
                                <p:inputTextarea styleClass="grid_20 ajustInput"  value="#{managerCreateDataSheet.dataSheet.obs}" />

                            </fieldset>
                        </p:panel>

                        <p:panel id="intPanel" style="background: none; border: none;" 
                                 styleClass="container_24 clearfix both ">
                            <fieldset>
                                <legend> <h4 style="margin:0 0 10px 0">Interessado</h4></legend>
                                <p:outputLabel value="Pessoa: "  styleClass="grid_3"/>
                                <p:selectOneRadio id="typePerson" style="margin-top:-8px;" styleClass="grid_8"
                                                value="#{managerCreateDataSheet.interested.typeDocument}" >  
                                    <f:selectItems value="#{managerCreateDataSheet.typeDocumentList()}" />
                                    <p:ajax event="change" update=":dataForm:intPanel" 
                                            />
                                </p:selectOneRadio>

                                <div class="clear"/>

                                <p:outputLabel id="typeDocumentLabel" 
                                                value="#{managerCreateDataSheet.interested.typeDocument}: " 
                                                styleClass="grid_3"/>

                                <p:inputMask id="typeDocumentInput" 
                                                value="#{managerCreateDataSheet.interested.typeDocument.type eq 'CPF' ? 
                                                        managerCreateDataSheet.interested.cpf : managerCreateDataSheet.interested.cnpj}"
                                                mask="#{managerCreateDataSheet.interested.typeDocument.type eq 'CPF' ? 
                                                        '999.999.999-99':'99.999.999/9999-99'}" 
                                                styleClass="grid_8" > 
                                    <p:ajax event="blur" update=":dataForm:intPanel" 
                                            listener="#{managerCreateDataSheet.existInterested()}" />
                                </p:inputMask>
                                <div class="clear"/>
                                <p:outputLabel value="Nome:" styleClass="grid_3" for="nomeInputText" 
                                                rendered="#{managerCreateDataSheet.interested.typeDocument.type eq 'CPF'}"/>
                                <p:inputText id="nomeInputText" styleClass="grid_20 ajustInput" value="#{managerCreateDataSheet.interested.name}" 
                                                rendered="#{managerCreateDataSheet.interested.typeDocument.type eq 'CPF'}" />

                                <p:outputLabel value="Fantasia:" styleClass="grid_3"
                                                for="nameFantasyInputText" rendered="#{managerCreateDataSheet.interested.typeDocument.type eq 'CNPJ'}" />
                                <p:inputText id="nameFantasyInputText" styleClass="grid_20 ajustInput"
                                                value="#{managerCreateDataSheet.interested.name_fantasy}" 
                                                rendered="#{managerCreateDataSheet.interested.typeDocument.type eq 'CNPJ'}" />
                            </fieldset>
                        </p:panel>
                        <p:panel id="realtyPanel" style="background: none; border: none;" 
                                 styleClass="container_24 clearfix both ">
                            <fieldset>
                                <legend> <h4 style="margin:0 0 10px 0">Dados Imóvel</h4></legend>

                                <p:outputLabel value="Propriedade:" styleClass="grid_3"  />
                                <div class="grid_8">
                                    <p:selectOneMenu value="#{managerCreateDataSheet.realty.typeProperty}">
                                        <f:selectItem itemLabel="" itemValue=""/>
                                        <f:selectItems value="#{managerCreateDataSheet.typeProperty}"/>  
                                    </p:selectOneMenu>
                                </div>
                                
                                <p:outputLabel value="Tipo Logradouro:" styleClass="grid_4 ajustLabel" />
                                <div class="grid_8" style="width: 27.5%">
                                    <p:selectOneMenu panelStyle="width:150px;" value="#{managerCreateDataSheet.realty.typeRealty}">
                                        <f:selectItem itemLabel="" itemValue=""/>
                                        <f:selectItems value="#{managerCreateDataSheet.typeRealty}"/>  
                                    </p:selectOneMenu>
                                </div>
                                
                                <p:outputLabel value="Logradouro:" styleClass="grid_3"  />
                                <p:inputText styleClass="grid_20 ajustInput"  value="#{managerCreateDataSheet.identificationRealty.patio}"/>
                                
                                <p:outputLabel value="Bairro:" styleClass="grid_3" />
                                <p:inputText styleClass="grid_20 ajustInput"  value="#{managerCreateDataSheet.identificationRealty.neighborhood}"/>
                                
                                <p:outputLabel value="Cep:" styleClass="grid_3"  />
                                <p:inputMask mask="999.999-99" styleClass="grid_8" value="#{managerCreateDataSheet.identificationRealty.cep}"/>
                                
                                <p:outputLabel value="País:" styleClass="grid_3"  />
                                <p:inputText styleClass="grid_8" value="#{managerCreateDataSheet.identificationRealty.nation}"/>

                                <p:outputLabel value="Titulo:" styleClass="grid_3"  />
                                <div class="grid_8">
                                    <p:selectOneMenu panelStyle="width:150px;" value="#{managerCreateDataSheet.realty.typeTitle}">
                                        <f:selectItem itemLabel="" itemValue=""/>
                                        <f:selectItems value="#{managerCreateDataSheet.typeTitle}"/>  
                                    </p:selectOneMenu>
                                </div>
                                
                                <p:outputLabel value="Área:" styleClass="grid_3 ajustLabel"  />
                                <p:inputText styleClass="grid_8" value="#{managerCreateDataSheet.identificationRealty.area}"/>
                                <div class="clear"/>
                                
                                <p:outputLabel value="Característica:" styleClass="grid_3" />
                                <p:inputTextarea styleClass="grid_20 ajustInput" value="#{managerCreateDataSheet.identificationRealty.feature}"/>
                            </fieldset>
                        </p:panel>


                        <p:panel id="dimensionPanel" style="background: none; border: none;" 
                                 styleClass="container_24 clearfix both ">
                            <fieldset>
                                <legend> <h4 style="margin:0 0 10px 0">Dimensões e Confrontações</h4></legend>

                                <p:outputLabel value="Frente:" styleClass="grid_3"  />
                                <p:inputText styleClass="grid_20 ajustInput" value="#{managerCreateDataSheet.identificationRealty.front}"/>
                               
                                <p:outputLabel value="Lado Direito:" styleClass="grid_3"  />
                                <p:inputText styleClass="grid_20 ajustInput" value="#{managerCreateDataSheet.identificationRealty.right_identification}"/>
                                
                                <p:outputLabel value="Lado Esquerdo:" styleClass="grid_3 leftSideLabel" />
                                <p:inputText styleClass="grid_20 ajustInput" value="#{managerCreateDataSheet.identificationRealty.left_identification}"/>

                                <p:outputLabel value="Fundo:" styleClass="grid_3"  />
                                <p:inputText styleClass="grid_20 ajustInput" value="#{managerCreateDataSheet.identificationRealty.ground}"/>
                            </fieldset>
                        </p:panel>

                        <p:panel id="TransmitentePanel" style="background: none; border: none;" 
                                 styleClass="container_24 clearfix both ">
                            <fieldset>
                                <legend> <h4 style="margin:0 0 10px 0">Transmitente</h4></legend>
                                <p:outputLabel value="Pessoa: "  styleClass="grid_3"/>
                                <p:selectOneRadio id="typePersonTransmitente" style="margin-top:-8px;" styleClass="grid_8"
                                                  value="#{managerCreateDataSheet.transmitente.typeDocument}" >  
                                    <f:selectItems value="#{managerCreateDataSheet.typeDocumentList()}" />
                                    <p:ajax event="change" update=":dataForm:peopleTransmitentePanel" 
                                            />
                                </p:selectOneRadio>

                                <div class="clear"/>

                                <p:panel id="peopleTransmitentePanel" 
                                         style="background: none; border: none; margin-left: -7px; margin-top: -20px;" 
                                         styleClass="container_24 clearfix both " >

                                    <div class="clear"/>

                                    <p:outputLabel id="typeDocumentLabelTransmitente" 
                                                   value="#{managerCreateDataSheet.transmitente.typeDocument}: " 
                                                   styleClass="grid_3"/>

                                    <p:inputMask id="typeDocumentInputTransmitente" 
                                                 value="#{managerCreateDataSheet.transmitente.typeDocument.type eq 'CPF' ? 
                                                          managerCreateDataSheet.transmitente.cpf : managerCreateDataSheet.transmitente.cnpj}"
                                                 mask="#{managerCreateDataSheet.transmitente.typeDocument.type eq 'CPF' ? 
                                                         '999.999.999-99':'99.999.999/9999-99'}" 
                                                 styleClass="grid_8" > 
                                        <p:ajax event="blur" update=":dataForm:peopleTransmitentePanel" 
                                                listener="#{managerCreateDataSheet.existTransmitente()}" />
                                    </p:inputMask>
                                    <div class="clear"/>
                                    <p:outputLabel value="Nome:" styleClass="grid_3" for="nomeTransInputText" 
                                                   rendered="#{managerCreateDataSheet.transmitente.typeDocument.type eq 'CPF'}"/>
                                    <p:inputText id="nomeTransInputText" styleClass="grid_20 ajustInput" value="#{managerCreateDataSheet.transmitente.name}" 
                                                 rendered="#{managerCreateDataSheet.transmitente.typeDocument.type eq 'CPF'}" />

                                    <p:outputLabel value="Fantasia:" styleClass="grid_3"
                                                   for="nameFantasyInputText" rendered="#{managerCreateDataSheet.transmitente.typeDocument.type eq 'CNPJ'}" />
                                    <p:inputText id="nameFantasyTransInputText" styleClass="grid_20 ajustInput"
                                                 value="#{managerCreateDataSheet.transmitente.name_fantasy}" 
                                                 rendered="#{managerCreateDataSheet.transmitente.typeDocument.type eq 'CNPJ'}" />

                                    <br/>
                                    <div class="buttonAction">
                                        <p:commandButton value="Adicionar" icon="ui-icon-plus"
                                                         update="@form, transmitenteTable"
                                                         actionListener="#{managerCreateDataSheet.addTransmitente()}"
                                                         />

                                        <p:button href="indexphysical.xhtml" value="Cancelar"  icon="ui-icon-trash" />
                                    </div >
                                    <br/>
                                    <p:dataTable var="cht" id="transmitenteTable" value="#{managerCreateDataSheet.transmitentes}"  
                                                 emptyMessage="Nenhum registro encontrado" rowIndexVar="rowid"
                                                 rowKey="#{cht.id}"
                                                 paginatorPosition="bottom" paginator="true" rows="5"
                                                 >
                                        <f:facet name="header">
                                            Transmitente
                                        </f:facet> 
                                        <p:column headerText="Ação" style="width: 50px; vertical-align: middle;">  
                                            <p:commandButton icon="ui-icon-trash"  update="@form"
                                                             actionListener="#{managerCreateDataSheet.removeTransmitente(rowid)}"   
                                                             style="width: 30px;" 
                                                             title="excluir"/>

                                        </p:column> 
                                        <p:column headerText="Nome / Fantasia" >  
                                            <h:outputLabel value="#{cht.name}" rendered="#{cht.typeDocument.type eq 'CPF'}" />  
                                            <h:outputLabel value="#{cht.name_fantasy}" rendered="#{cht.typeDocument.type eq 'CNPJ'}" />  

                                        </p:column>
                                        <p:column headerText="Tipo de Documento" >  
                                            <h:outputLabel value="#{cht.typeDocument}" />  
                                        </p:column>
                                    </p:dataTable>
                                </p:panel>
                            </fieldset>
                        </p:panel>

                        <p:panel id="adquirentePanel" style="background: none; border: none;" 
                                 styleClass="container_24 clearfix both ">
                            <fieldset>
                                <legend> <h4 style="margin:0 0 10px 0">Adquirente</h4></legend>
                                <p:outputLabel value="Pessoa: "  styleClass="grid_3"/>
                                <p:selectOneRadio id="typePersonAdquirente" style="margin-top:-8px;" styleClass="grid_8"
                                                  value="#{managerCreateDataSheet.adquirente.typeDocument}" >  
                                    <f:selectItems value="#{managerCreateDataSheet.typeDocumentList()}" />
                                    <p:ajax event="change" update=":dataForm:peopleAdquirentePanel" 
                                            />
                                </p:selectOneRadio>

                                <div class="clear"/>

                                <p:panel id="peopleAdquirentePanel" 
                                         style="background: none; border: none; margin-left: -7px; margin-top: -20px;" 
                                         styleClass="container_24 clearfix both " >


                                    <div class="clear"/>

                                    <p:outputLabel id="typeDocumentLabelAdquirente" 
                                                   value="#{managerCreateDataSheet.adquirente.typeDocument}: " 
                                                   styleClass="grid_3"/>

                                    <p:inputMask id="typeDocumentInputAdquirente" 
                                                 value="#{managerCreateDataSheet.adquirente.typeDocument.type eq 'CPF' ? 
                                                          managerCreateDataSheet.adquirente.cpf : managerCreateDataSheet.adquirente.cnpj}"
                                                 mask="#{managerCreateDataSheet.adquirente.typeDocument.type eq 'CPF' ? 
                                                         '999.999.999-99':'99.999.999/9999-99'}" 
                                                 styleClass="grid_8" > 
                                        <p:ajax event="blur" update=":dataForm:peopleAdquirentePanel" 
                                                listener="#{managerCreateDataSheet.existAdquirente()}" />
                                    </p:inputMask>
                                    <div class="clear"/>
                                    <p:outputLabel value="Nome: " styleClass="grid_3" for="nomeAdqInputText" 
                                                   rendered="#{managerCreateDataSheet.adquirente.typeDocument.type eq 'CPF'}"/>
                                    <p:inputText id="nomeAdqInputText" styleClass="grid_20 ajustInput" value="#{managerCreateDataSheet.adquirente.name}" 
                                                 rendered="#{managerCreateDataSheet.adquirente.typeDocument.type eq 'CPF'}" />

                                    <p:outputLabel value="Fantasia: " styleClass="grid_3"
                                                   for="nameFantasyInputText" rendered="#{managerCreateDataSheet.adquirente.typeDocument.type eq 'CNPJ'}" />
                                    <p:inputText id="nameFantasyAdqInputText" styleClass="grid_20 ajustInput"
                                                 value="#{managerCreateDataSheet.adquirente.name_fantasy}" 
                                                 rendered="#{managerCreateDataSheet.adquirente.typeDocument.type eq 'CNPJ'}" />

                                    <br/>
                                    <div class="buttonAction">
                                        <p:commandButton value="Adicionar" icon="ui-icon-plus"
                                                         update="@form, adquirenteTable"
                                                         actionListener="#{managerCreateDataSheet.addAdquirente()}"
                                                         />

                                        <p:button href="indexphysical.xhtml" value="Cancelar"  icon="ui-icon-trash" />
                                    </div >
                                    <br/>
                                    <p:dataTable var="cht" id="adquirenteTable" value="#{managerCreateDataSheet.adquirentes}"  
                                                 emptyMessage="Nenhum registro encontrado" rowIndexVar="rowid"
                                                 rowKey="#{cht.id}"
                                                 paginatorPosition="bottom" paginator="true" rows="5"
                                                 >
                                        <f:facet name="header">
                                            Transmitente
                                        </f:facet> 
                                        <p:column headerText="Ação" style="width: 50px; vertical-align: middle;">  
                                            <p:commandButton icon="ui-icon-trash"  update="@form"
                                                             actionListener="#{managerCreateDataSheet.removeAdquirente(rowid)}"   
                                                             style="width: 30px;" 
                                                             title="excluir"/>

                                        </p:column> 
                                        <p:column headerText="Nome / Fantasia" >  
                                            <h:outputLabel value="#{cht.name}" rendered="#{cht.typeDocument.type eq 'CPF'}" />  
                                            <h:outputLabel value="#{cht.name_fantasy}" rendered="#{cht.typeDocument.type eq 'CNPJ'}" />  

                                        </p:column>
                                        <p:column headerText="Tipo de Documento" >  
                                            <h:outputLabel value="#{cht.typeDocument}" />  
                                        </p:column>
                                    </p:dataTable>
                                </p:panel>
                            </fieldset> 
                        </p:panel>
                        <p:panel id="anexoPanel" style="background: none; border: none;" 
                                 styleClass="container_24 clearfix both ">
                            <fieldset>
                                <legend> <h4 style="margin:0 0 10px 0">Anexo</h4></legend>
                                <p:fileUpload 
                                    mode="advanced"  
                                    label="Anexar"
                                    fileUploadListener="#{managerCreateDataSheet.handle}"
                                    multiple="true" 
                                    auto="true"
                                    invalidFileMessage="Tipo de arquivo não suportado."
                                    invalidSizeMessage="Tamanho do arquivo não é suportado"
                                    uploadLabel="Enviar"
                                    cancelLabel="Cancelar"
                                    immediate="true"
                                    update="messages, tableAnnex"
                                    allowTypes="/(\.|\/)(gif|tif|jpe?g|png|pdf)$/"/>

                                <div class="clear"/>
                                <br/>

                                <p:dataTable id="tableAnnex" var="a" value="#{managerCreateDataSheet.annexs}" 
                                             styleClass="grid_26"
                                             rowIndexVar="rowid"
                                             rowKey="#{a.id}" 
                                             rows="5" paginator="true"  
                                             paginatorPosition="bottom"
                                             emptyMessage="Nenhum registro">  

                                    <p:column headerText="Ação" width="20">
                                        <p:commandButton  style="width: 30px; height: 30px;" 
                                                          title="remover" update="@form"
                                                          actionListener="#{managerCreateDataSheet.removeAnnex(rowid,a)}"
                                                          icon="ui-icon-trash"/>
                                    </p:column>   

                                    <p:column width="600">  
                                        <h:outputLink  
                                            value="#{a.type ne 'application/pdf' ? 'viewImage.xhtml':'show'}" title="#{a.description}"  target="_blank" >

                                            <div style="text-align: center; width: 20%;" class="grid_4">

                                                <p:graphicImage styleClass="image grid_3" rendered="#{a.type ne 'application/pdf'}"
                                                                style="margin-left:1%; width: 50px;"
                                                                url="show/?annex=#{a.id}" />

                                                <p:outputLabel  value="#{a.description.length() ge 10 ? a.description.substring(0, 10).concat('...') : 
                                                                         a.description.replace('.png','').replace(' ','_')}" 
                                                                style="margin-left:1%; width: 100px;" rendered="#{a.type ne 'application/pdf'}"/>

                                                <p:graphicImage styleClass="image grid_3" rendered="#{a.type eq 'application/pdf'}"
                                                                style="margin-left:1%; width: 100px;" 
                                                                url="assets/images/pdf.jpeg" />


                                                <p:outputLabel  value="#{a.description.length() ge 10 ? a.description.substring(0, 10).concat('...') : 
                                                                         a.description.replace('.pdf','').replace(' ','_')}" 
                                                                style="margin-left:1%; width: 100px;" rendered="#{a.type eq 'application/pdf'}"/>
                                            </div>
                                            <f:param name="annex" value="#{a.id}"/>
                                        </h:outputLink>

                                    </p:column>  

                                </p:dataTable> 
                            </fieldset>
                        </p:panel>
                        <br/>
                        <div class="buttonAction">
                            <p:commandButton value="Salvar" icon="ui-icon-disk"
                                             update="@form"
                                             actionListener="#{managerCreateDataSheet.save()}"
                                             styleClass="ui-priority-primary" />

                            <p:button href="indexdatasheet.xhtml" value="Cancelar"  icon="ui-icon-trash" />
                        </div >
                    </div>
                </div>
            </h:form>
        </ui:define>
    </ui:composition>

</html>
